#include "QueryPatientUi.h"
#include "ui_QueryPatientUi.h"
#include "WidgetHandler.h"
#include "BootSetting.h"
#include "MiscData.h"
#include "Database.h"
#include "HintDialog.h"
#include "Medio.h"
#include "LogoUtility.h"
#include "Mcu.h"

static const char *tablefiled[] = {"number",  "id", "name",
                                  "age", "birthday", "gender",
                                  "visitTime", "phone", "addr"};



QueryPatientUi::QueryPatientUi(QWidget *parent) :
    BaseWidget(parent),
    ui(new Ui::QueryPatientUi)
{
    ui->setupUi(this);
    LogoUtility::getInstance()->addLogoLabel(ui->logo);
    m_querytype = "number";
    m_querystr = "%1";
    g_infoArray[0] = ui->info0;
    g_infoArray[1] = ui->info1;
    g_infoArray[2] = ui->info2;
    g_infoArray[3] = ui->info3;
    g_infoArray[4] = ui->info4;
    g_infoArray[5] = ui->info5;
    g_infoArray[6] = ui->info6;
    g_infoArray[7] = ui->info7;
    g_infoArray[8] = ui->info8;
    g_infoArray[9] = ui->info9;

    auto list = ui->widget_2->findChildren<QPushButton*>();
    int i = 0;
    for(auto &item:list){
        m_buttongroup.addButton(item,i++);
    }

    for (int i = 0; i < PATIENT_RECORD_SIZE; i++) {
        g_infoArray[i]->setNumber(-1);
        g_infoArray[i]->setIndex(i);
        g_infoArray[i]->hide();
        QObject::connect(g_infoArray[i], &PatientRecordItem::clickedSignal, this, &QueryPatientUi::clickedSlot);
    }

}

QueryPatientUi::~QueryPatientUi()
{
    delete ui;
}

void QueryPatientUi::activate()
{
    /* 清空ui上所有患者信息 */
    clearPatientRecord();
    queryAllPatientInfo();
    ui->pushButton_2->click();
}

void QueryPatientUi::inactivate()
{

}

void QueryPatientUi::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;

    default:
        break;
    }
}

/* 点击患者记录信息 */
void QueryPatientUi::clickedSlot(int index)
{
    Mcu::getInstance()->touchSoundBeepMessage();
    int number = g_infoArray[index]->getNumber();

    qDebug()<<"index = "<<index<<", number = "<<number;
    if (number < 0)
        return;
    RuntimeData::MiscData::getInstance()->g_miscDataInfo.currentSelectPatientNumber = number;
    RuntimeData::MiscData::getInstance()->g_miscDataInfo.patientInfoEntryType = 1;
    WidgetHandler::getInstance()->displayWidget(PatientInfoUi_Id);
}

void QueryPatientUi::on_query_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    /* 清空ui上所有患者信息 */
    clearPatientRecord();

    if (ui->condition->text().isNull() || ui->condition->text().isEmpty()) {
        if (queryAllPatientInfo() == 0) {
            HintDialog::getInstance()->setInfo(QString(tr("查询完毕!")), HintDialogType_Hint);
            HintDialog::getInstance()->exec();
        }
    } else {
        clearList();
        QString value = ui->condition->text();
        QString temp("%1 LIKE \"%2\" OR ");
        QString query;
        for(int i = 0;i<3;++i) {
            query+=temp.arg(tablefiled[i+1]).arg(value);
        }
        query.chop(3);

        m_list += Database::getInstance()->queryPatient(query)->toVector();
        if (m_list.isEmpty()) {
            HintDialog::getInstance()->setInfo(tr("未查询到患者, 请新建患者!"), HintDialogType_Hint);
            HintDialog::getInstance()->exec();
        }
        m_currentPageIndex = 0;
        m_pages = m_list.length()?m_list.length()/10+(m_list.length()%10?1:0):0;
        qDebug()<<"m_list.length():"<<m_list.length();
        updatePatientList();
    }
}

void QueryPatientUi::clearPatientRecord()
{
    for (int i = 0; i < PATIENT_RECORD_SIZE; i++) {
        g_infoArray[i]->setNumber(-1);
        g_infoArray[i]->setId("");
        g_infoArray[i]->setName("");
        g_infoArray[i]->setAge(-1);
        g_infoArray[i]->setBirthday("");
        g_infoArray[i]->setGender(-1);
        g_infoArray[i]->setVisitTime("");
        g_infoArray[i]->setPhone("");
        g_infoArray[i]->setAddr("");
    }
}

int QueryPatientUi::queryOnePatientByNumber(int number)
{
    /* 通过number查询 */
    PatientInfo* info = Database::getInstance()->queryPatient(number);
    if (info->number < 0) {
        HintDialog::getInstance()->setInfo(QString(tr("未查询到编号为%1的患者!")).arg(number), HintDialogType_Hint);
        HintDialog::getInstance()->exec();
        return -1;
    }

    /* 更新ui */
    g_infoArray[0]->setNumber(info->number);
    g_infoArray[0]->setId(info->id);
    g_infoArray[0]->setName(info->name);
    g_infoArray[0]->setAge(info->age);
    g_infoArray[0]->setBirthday(info->birthday);
    g_infoArray[0]->setGender(info->gender);
    g_infoArray[0]->setVisitTime(info->visitTime);
    g_infoArray[0]->setPhone(info->phone);
    g_infoArray[0]->setAddr(info->addr);

    return 0;
}

int QueryPatientUi::queryAllPatientInfo()
{
    /* 查询所有患者 */
    clearList();
    m_list = Database::getInstance()->queryPatient()->toVector();
    if (m_list.isEmpty()) {
        HintDialog::getInstance()->setInfo(tr("未查询到患者, 请新建患者!"), HintDialogType_Hint);
        HintDialog::getInstance()->exec();
       // return -1;
    }
    m_currentPageIndex = 0;
    m_pages = m_list.length()?m_list.length()/10+(m_list.length()%10?1:0):0;
    qDebug()<<m_list.length();
    updatePatientList();

    return 0;
}

void QueryPatientUi::updatePatientList(){
    /* 更新ui */
    int i = m_currentPageIndex*10;
    int end = m_currentPageIndex*10+10;
    int index = 0;
    for (i; i<end &&i<m_list.length(); ++i,++index) {
        g_infoArray[index]->show();
        g_infoArray[index]->setNumber(m_list.at(i)->number);
        g_infoArray[index]->setId(m_list.at(i)->id);
        g_infoArray[index]->setName(m_list.at(i)->name);
        g_infoArray[index]->setAge(m_list.at(i)->age);
        g_infoArray[index]->setBirthday(m_list.at(i)->birthday);
        g_infoArray[index]->setGender(m_list.at(i)->gender);
        g_infoArray[index]->setVisitTime(m_list.at(i)->visitTime);
        g_infoArray[index]->setPhone(m_list.at(i)->phone);
        g_infoArray[index]->setAddr(m_list.at(i)->addr);
    }
    for(index;index<10;++index){
        g_infoArray[index]->hide();
        g_infoArray[index]->clear();
    }
    ui->pageInfo->setText(QString("%1/%2").arg(m_currentPageIndex+1).arg(m_pages));
}

void QueryPatientUi::clearList() {
    for(int i = 0;i<m_list.length();++i) {
        PatientInfo* item = m_list.at(i);
        delete item;
    }
    m_list.clear();
}

void QueryPatientUi::on_gohome_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void QueryPatientUi::on_goback_clicked()
{
    Mcu::getInstance()->touchSoundBeepMessage();
    WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
}

void QueryPatientUi::on_queryPatient_3_clicked(){
    Mcu::getInstance()->touchSoundBeepMessage();
    if(m_pages>0){
        if(++m_currentPageIndex>=m_pages){
            m_currentPageIndex = 0;
        }
        updatePatientList();
    }

}

void QueryPatientUi::on_queryPatient_2_clicked(){
    Mcu::getInstance()->touchSoundBeepMessage();
    if(m_pages>0){
        if(--m_currentPageIndex<0){
            m_currentPageIndex = m_pages-1;
        }
        updatePatientList();
    }

}
